<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form inline size="small">
            <el-form-item label="题库分类：">
              <el-select
                v-model="listPram.cid"
                clearable
                class="selWidth"
                placeholder="请选择文章分类"
                @change="handerSearch"
              >
                <el-option
                  v-for="item in categoryTreeData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="关键词：">
              <el-input
                v-model="listPram.keywords"
                placeholder="请输入关键词"
                class="selWidth"
                size="small"
                clearable
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handerSearch"
                  size="small"
                />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <router-link :to="{ path: '/course/create' }">
          <el-button
            size="small"
            type="primary"
            v-hasPermi="['admin:article:save']"
            >添加题目</el-button
          >
        </router-link>
        <el-button
          size="small"
          type="primary"
          v-hasPermi="['admin:article:save']"
          @click="openDialog"
          >导入题目</el-button
        >
        <el-button
          size="small"
          v-hasPermi="['admin:article:save']"
          @click="downLoad"
          >下载题目模板</el-button
        >
      </div>
      <el-table
        v-loading="listLoading"
        :data="listData.list"
        size="mini"
        class="table"
        highlight-current-row
        :header-cell-style="{ fontWeight: 'bold' }"
      >
        <el-table-column prop="id" label="编号" min-width="50" />
        <el-table-column prop="title" label="标题" min-width="180" />
        <el-table-column prop="visit" label="分类" min-width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.cid | articleTypeFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序(正序)" min-width="180" />
        <el-table-column prop="updateTime" label="更新时间" min-width="180" />
        <el-table-column
          label="操作"
          min-width="100"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope">
            <router-link :to="{ path: '/course/create/' + scope.row.id }">
              <el-button
                size="small"
                type="text"
                class="mr10"
                v-hasPermi="['admin:article:info']"
                >编辑题目</el-button
              >
            </router-link>
            <el-button
              type="text"
              size="small"
              @click="handlerDelete(scope.row)"
              v-hasPermi="['admin:article:delete']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog   title="导入题目"  :visible.sync="dialogVisible" :before-close="close">
        <el-upload
          drag
          multiple
          :auto-upload="false"
          :file-list="fileList"
          :on-remove="removeFile"
          :on-change="updateFileList"
          limit="1"
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        </el-upload>
        <div class="footer_button">
          <el-button type="primary" @click="uploadExcel">确定</el-button>
           <el-button @click="close">取消</el-button>
        </div>
      </el-dialog>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import * as articleApi from "@/api/article.js";
import * as categoryApi from "@/api/categoryApi.js";
import * as selfUtil from "@/utils/ZBKJIutil.js";
import { exportExcel,importExcel } from "@/api/excel";
import  axios  from  'axios'
import edit from "./edit";
export default {
  // name: "list",
  components: {
    edit,
  },
  data() {
    return {
      fileList:[],
      dialogVisible:false,
      constants: this.$constants,
      listPram: {
        keywords: null,
        cid: null,
        page: 1,
        limit: this.$constants.page.limit[0],
      },
      listData: {
        list: [],
        total: 0,
      },
      editDialogConfig: {
        visible: false,
        data: {},
        isEdit: 0, // 0=add 1=edit
      },
      listLoading: true,
      categoryTreeData: [],
      categoryProps: {
        value: "id",
        label: "name",
        children: "child",
        expandTrigger: "hover",
        checkStrictly: true,
        emitPath: false,
      },
    };
  },
  mounted() {
    this.handlerGetListData(this.listPram);
    this.handlerGetTreeList();
  },
  methods: {
    uploadExcel(){
      const formData = new FormData();
      formData.append("file", this.fileList[0].raw);
      console.log(formData,this.fileList)
      importExcel(formData).then(res=>{
        console.log(res)
      })
    },
    close(){
      this.dialogVisible=false
      this.fileList=[]
    },
    removeFile(file,fileList){
    this.fileList=fileList
      console.log(fileList)
    },
    updateFileList(file,fileList){
    this.fileList=fileList
      console.log(fileList)
    },
    openDialog() {
      this.dialogVisible=true
    },
    importExcel(e) {
      console.log(e);
    },
    downLoad() {
      exportExcel({ articleType: 0 }).then((res) => {
        console.log(res);
        window.location.href = res.fileName;
      });
    },
    handlerGetTreeList() {
      categoryApi
        .listCategroy({
          type: 3,
          status: "",
        })
        .then((data) => {
          this.categoryTreeData = data.list;
          localStorage.setItem("articleClass", JSON.stringify(data.list));
        });
    },
    handerSearch() {
      this.listPram.page = 1;
      this.handlerGetListData(this.listPram);
    },
    handlerGetListData(pram) {
      this.listLoading = true;
      articleApi.ListArticle(pram).then((data) => {
        this.listData = data;
        this.listLoading = false;
      });
    },
    handlerOpenEdit(isEdit, editData) {
      // 0=add 1=edit
      if (isEdit === 1) {
        this.editDialogConfig.isEdit = 1;
        this.editDialogConfig.editData = editData;
      } else {
        this.editDialogConfig.isEdit = 0;
      }
      this.editDialogConfig.visible = true;
    },
    handlerHideDialog() {
      this.handlerGetListData(this.listPram);
      this.editDialogConfig.visible = false;
    },
    handlerDelete(rowData) {
      this.$confirm("确定删除当前数据", "提示").then((result) => {
        articleApi.DelArticle(rowData).then((data) => {
          this.$message.success("删除数据成功");
          this.handlerGetListData(this.listPram);
        });
      });
    },
    handleSizeChange(val) {
      this.listPram.limit = val;
      this.handlerGetListData(this.listPram);
    },
    handleCurrentChange(val) {
      this.listPram.page = val;
      this.handlerGetListData(this.listPram);
    },
  },
};
</script>

<style scoped lang="scss">
.articleModal {
  z-index: 333 !important;
}
::v-deep .el-dialog__body{
  text-align: center !important;
}
.footer_button{
 text-align: right;
  margin: 20px 0 10px 0;
}
</style>
